package com.fsj.miniapp.shiro;

import com.fsj.miniapp.model.User;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.SimpleCredentialsMatcher;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.apache.shiro.crypto.hash.SimpleHash;

public class CredentialMatcher extends SimpleCredentialsMatcher {


    @Override
    public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {
        User userDB =  (User) info.getPrincipals().asList().get(0);


        UsernamePasswordToken usernamePasswordToken = (UsernamePasswordToken) token;

        String pwd =new SimpleHash(Sha256Hash.ALGORITHM_NAME, usernamePasswordToken.getPassword(),userDB.getSalt() , 1024).toString();
        return this.equals(pwd, info.getCredentials().toString());
    }
}
